package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.enums.DubboServiceEnum;
import cc.rengu.igas.bsps.common.enums.MompBizProdCodeEnum;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.common.util.ChangeDubboUtil;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.momp.facade.bean.McProductInfoBean;
import cc.rengu.igas.momp.facade.request.MchntMcManageRequest;
import cc.rengu.igas.momp.facade.response.MchntMcManageResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * @author Xuegui Cao
 * @date 2020/6/30
 * @description 商户营销活动管理
 */
public class ManageMchntMcService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        MchntMcManageRequest request = new MchntMcManageRequest();
        // 读取存在xml树里的实体类
        ConvertUtil.convertOutput(request);
        return request;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntMcManageRequest dubboRequest = (MchntMcManageRequest) request;
        dubboRequest.setQueryOrgId(ChangeDubboUtil.getSignInstId());

        /*新增活动时,需要对营销产品编码进行转换;修改时不需要*/
        if (StringUtil.isEmptyOrNull(dubboRequest.getMcInfoObj().getMcNo())) {
            List<McProductInfoBean> productInfoList = dubboRequest.getProductInfoList();
            dubboRequest.setProductInfoList(getProductInfoList(productInfoList));
        }
        //修改请求Header
        ChangeDubboUtil.changeHeader(dubboRequest.getHeader(), DubboServiceEnum.M00MO026.getTxnNum());

        //调用Dubbo，获取结果集和相应对象。
        DubboService dubboService = new DubboServiceImpl();
        Result<MchntMcManageResponse> result = (Result<MchntMcManageResponse>) dubboService.callDubboService(DubboServiceEnum.M00MO026.getServerName(), DubboServiceEnum.M00MO026.getMethodName(), dubboRequest);
        if (result != null && result.isSuccess()) {
            //调用成功
            BizResponse<MchntMcManageResponse> bizResponse = new BizResponse<>();
            ChangeDubboUtil.setBizResponse(bizResponse, AppParamConstant.SYS_ID, result);
            return bizResponse;
        } else {
            rglog.error("【商户服务】调用【{}】服务异常！", DubboServiceEnum.M00MO026.getDescription());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntMcManageResponse modelRespose = (MchntMcManageResponse) bizResponse.getResult();
        ConvertUtil.convertInput(modelRespose);
    }

    /**
     * 转换营销产品编码
     *
     * @param productInfoList 上送商户产品信息
     * @return 营销产品编码
     */
    private List<McProductInfoBean> getProductInfoList(List<McProductInfoBean> productInfoList) {
        if (CollectionUtils.isEmpty(productInfoList)) {
            return null;
        }
        List<McProductInfoBean> realProductInfoList = new ArrayList<>();
        HashSet<String> productSet = new HashSet<>();
        for (McProductInfoBean mcProductInfoBean : productInfoList) {
            String key = mcProductInfoBean.getBizProdCode();
            rglog.debug("-----上送产品代码<{}>", key);
            if (StringUtil.isEmptyOrNull(key)) {
                continue;
            }
            if (key.endsWith("*")) {
                /**/
                key = key.substring(0, 10);
                for (MompBizProdCodeEnum mompBizProdCodeEnum : MompBizProdCodeEnum.values()) {
                    if (mompBizProdCodeEnum.getKey().contains(key)) {
                        rglog.debug("+++++转换营销产品编码<{}>", mompBizProdCodeEnum.getMompProdCode());
                        productSet.add(mompBizProdCodeEnum.getMompProdCode());
                        // McProductInfoBean mompProductInfoBean = new McProductInfoBean();
                        // mompProductInfoBean.setBizProdCode(mompBizProdCodeEnum.getMompProdCode());
                        // realProductInfoList.add(mompProductInfoBean);
                    }
                }
            } else {
                // key.endsWith("YYYYYYYYYY")
                int shiftTime = 0;
                boolean isOtherBankCard = false;
                for (int index = 0; 10 + index < key.length(); index++) {
                    String txnNumAndBizType = key.substring(0, 10);
                    if (key.charAt(10 + index) == 'Y') {
                        if (index < 4) {
                            //前5位 本行卡
                            txnNumAndBizType = txnNumAndBizType.concat("0").concat(String.valueOf(index));
                        } else {
                            //后5位 他行卡
                            if (isOtherBankCard) {
                                //已检查他行卡
                                break;
                            }
                            txnNumAndBizType = txnNumAndBizType.concat("10");
                            isOtherBankCard = true;
                        }
                        String mompProdCode = MompBizProdCodeEnum.getMompProdCodeByKey(txnNumAndBizType);
                        if (!StringUtil.isEmptyOrNull(mompProdCode)) {
                            rglog.debug("+++++转换营销产品编码<{}>", mompProdCode);
                            productSet.add(mompProdCode);
                            shiftTime++;
                        }
                        // McProductInfoBean mompProductInfoBean = new McProductInfoBean();
                        // mompProductInfoBean.setBizProdCode(mompProdCode);
                        // realProductInfoList.add(mompProductInfoBean);
                    }
                }
                //转换次数为0,尝试转换不区分账号类型的营销产品编码
                if (0 == shiftTime) {
                    key = key.substring(0, 10);
                    String mompProdCode = MompBizProdCodeEnum.getMompProdCodeByKey(key);
                    if (!StringUtil.isEmptyOrNull(mompProdCode)) {
                        rglog.debug("+++++转换营销产品编码<{}>", mompProdCode);
                        productSet.add(mompProdCode);
                    }
                }
            }
        }

        for (String product : productSet) {
            McProductInfoBean mompProductInfoBean = new McProductInfoBean();
            mompProductInfoBean.setBizProdCode(product);
            realProductInfoList.add(mompProductInfoBean);
        }
        rglog.debug("转换后产品列表<{}>", JSONObject.toJSONString(realProductInfoList));
        return realProductInfoList;
    }
}
